package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.AlipayUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnUtils;
import com.ruoyi.common.utils.property.WeChatProperty;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.AH5vipTrans;
import com.ruoyi.system.domain.AH5vipTransSub;
import com.ruoyi.system.domain.AHousing;
import com.ruoyi.system.domain.AOrder;
import com.ruoyi.system.mapper.AH5vipTransMapper;
import com.ruoyi.system.mapper.AH5vipTransSubMapper;
import com.ruoyi.system.mapper.AHousingMapper;
import com.ruoyi.system.mapper.AOrderMapper;
import com.ruoyi.system.service.AliPayService;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class AliPayServiceImpl implements AliPayService {

    private static String  appUrl = SpringUtils.getBean(WeChatProperty.class).getAppUrl();

    @Autowired
    private AOrderMapper aOrderMapper;

    @Autowired
    private AlipayUtils alipayUtils;

    @Autowired
    private AHousingMapper aHousingMapper;

    @Autowired
    private AH5vipTransMapper ah5vipTransMapper;

    @Autowired
    private AH5vipTransSubMapper ah5vipTransSubMapper;

    @Override
    public Object walletPay(AOrder aOrder) {
        Object result = null;
        aOrder.setUserId(LoginUtils.getUser().getId());
        aOrder.setCreateTime(DateUtils.getNowDate());
        String no = SnUtils.orderNo();
        aOrder.setOrderNo(no);
        String str = "H5转让发布";
        BigDecimal total = new BigDecimal("0.00");

        AH5vipTrans tr = ah5vipTransMapper.selectAH5vipTransById(aOrder.getVipId());
        total = tr.getPrice();
        aOrder.setOrPrice(total);
        if(aOrder.getTopId() != null){
            AH5vipTransSub sub = ah5vipTransSubMapper.selectAH5vipTransSubById(aOrder.getTopId());
            total = total.add(sub.getPrice());
            aOrder.setTopMoney(sub.getPrice());
        }

        aOrder.setTotalMoney(total);

        if(aOrder.getIsType().equals("4")){
            str = "H5出租发布";
        }
        aOrder.setTypeName(str);
        aOrder.setPayType("2");
        aOrderMapper.insertAOrder(aOrder);
        result = alipayUtils.alipay_h5(str, no, total, appUrl + "xcx/aliPay/AOrderAliPayCallBack?type=1");
        System.out.println(result);
        return result;
    }

    @Override
    public void checkOrderNo(String outTradeNo, String tradeNo) {
        AOrder order = aOrderMapper.selectAOrderByOrderNo(outTradeNo);
        order.setPayNo(tradeNo);//支付单号
        order.setPayStatus(2L);//支付完成
        order.setPayTime(DateUtils.getNowDate());
        aOrderMapper.updateAOrder(order);
        AHousing housing = aHousingMapper.selectAHousingById(order.getHousId());
        if(housing != null){
            housing.setIsPay("0");
            housing.setPayTime(DateUtils.getNowDate());

            if(order.getTopId() != null){
                AH5vipTransSub sub = ah5vipTransSubMapper.selectAH5vipTransSubById(order.getTopId());
                Date date = getDate(sub.getDes() == null ? 0 :sub.getDes());
                housing.setTopTime(date);
                housing.setIsTop("0");
            }
            housing.setTopLevel("0");
            aHousingMapper.updateAHousing(housing);
        }
    }

    private Date getDate(Long num) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusThirtyDays = now.plusDays(num);
        return Date.from(plusThirtyDays.atZone(ZoneId.systemDefault()).toInstant());
    }
}
